/**
 * 所有版权归 广西梧州 陈锦韬 所有
 *
 * @Title: DoubleInnerParameter
 * @Package com.rx.core.dbase.mutil
 * @Description: 双表内联
 * @author: 陈锦韬
 * @date: 2021\7\22 0022
 * @version V1.0
 * @Copyright: 2021 陈锦韬  All rights reserved.
 */
package com.rx.core.dbase.mutil.param;

import com.rx.core.base.RxBaseData;
import com.rx.core.util.EmptyChecker;
import com.rx.core.util.RxUtil;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Field;
import java.nio.file.Watchable;

/**
 * @author: Administrator
 * @Description: 双表内联
 * @date: 2021\7\22 0022
 */
@Data
@EqualsAndHashCode(callSuper = true)
@Slf4j
public class DoubleUnionParameter extends MutilTableParameter{

    Field replaceFieldClass;
    StringBuilder replaceSql;
    String replaceField;

    Field replaceRelationFieldClass;
    StringBuilder replaceRelationSql;
    String replaceRelationField;

    DoubleUnionParameter(RxBaseData rxBaseData) {
        super(rxBaseData);
    }

    @Override
    protected void dealRelationInfo(Field field, StringBuilder whereSqlTmp,StringBuilder relationSqlTmp){

        String columnName = RxUtil.StringUtil.humpToLine(field.getName());
        if (EmptyChecker.isEmpty(whereSqlTmp.toString())){
            StringBuilder object = otherConditon.get(columnName.toLowerCase());
            if (EmptyChecker.notEmpty(object)){
                whereSqlTmp = object;
            }
        }

        if(EmptyChecker.notEmpty(whereSqlTmp.toString())){
            replaceSql = whereSqlTmp;
            replaceField =  columnName.toUpperCase();
        }else {
            replaceFieldClass = field;
        }

        if(EmptyChecker.notEmpty(relationSqlTmp.toString())){
            replaceRelationSql = relationSqlTmp;
            replaceRelationField =  columnName.toUpperCase();
        }else {
            replaceRelationFieldClass = field;
        }
    }

    public String getRepalceSql(String sql){


        String newSql = replaceSql.toString()
                .replace(replaceField, RxUtil.StringUtil.humpToLine(replaceFieldClass.getName()).toUpperCase());
        newSql = onSql.toString().replace(replaceSql.toString(),newSql);

        String newRelationSql = replaceRelationSql.toString()
                .replace(replaceRelationField,RxUtil.StringUtil.humpToLine(replaceRelationFieldClass.getName()).toUpperCase());
        newSql = newSql.replace(replaceRelationSql,newRelationSql);
        // 替换掉

        return String.format(sql, selectSql,mainTable + " main", secondTable + " secondary",newSql,onSql,whereSql);
    }

}
